﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{

    public partial class Admin_ChiTietPhieuBanHang : BasePage
    {

        #region Fields
        private int indexGenControl = 0;
        private string ctrAutoComplete = string.Empty;
        public string urlList = "DMPhieuBanHang.aspx";
        List<ChiTietPhieuBanHangResult> lstChiTiet = new List<ChiTietPhieuBanHangResult>();
        public string CurencyName = string.Empty;
        #endregion

        #region ViewStates
        public string Command
        {

            get
            {
                object o = ViewState["Command"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["Command"] = value; }

        }
        public string userNameLogin
        {

            get
            {
                object o = ViewState["userNameLogin"];
                if (o != null) return (string)o;
                return string.Empty;
            }

            set { ViewState["userNameLogin"] = value; }

        }

        public Int64 MaPhieuBan
        {

            get
            {
                object o = ViewState["MaPhieuBan"];
                if (o != null) return (Int64)o;
                return Int64.MinValue;
            }

            set { ViewState["MaPhieuBan"] = value; }

        }
        #endregion

        #region Init,Loads
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(WebTeamplate.Admin_ChiTietPhieuBanHang));
            // ClientDefaultButton.SetPageDefaultButton(this.Page, btnSave.ClientID);
            if (!Page.IsPostBack)
            {
                InitParamater();
                InitTextControl();
                CommonFunction.LoadSubCate(drlKho, CommonFunction.DEFAULT_WAREHOUSE_CODE, false);
                if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
                {
                    GenarateListFormInsert();
                }
                else
                {
                    LoadData();
                    GenarateListFormUpdate();
                }
                //   drlKho.Focus();
                InitAutoCompleteData();
            }
        }
        private void GetCurency()
        {
            Curency objCurency = CurencyUtil.Get(CommonFunction.DEFAULT_VND_CURENCY);
            CurencyName = objCurency.Name;
        }
        private void LoadData()
        {
            GetCurency();
            PhieuBanHang objPhieuBanHang = PhieuBanHangUtil.Get(this.MaPhieuBan);
            if (objPhieuBanHang != null)
            {
                drlKho.SelectedValue = objPhieuBanHang.MaKho.ToString();
                txtNgayLapPhieu.Text = ConvertValueObject.ParseString(objPhieuBanHang.NgayLapPhieu);
                MembershipUser user = Membership.GetUser(objPhieuBanHang.NguoiLapPhieu);
                if (user != null)
                {
                    UserProfile profile = UserProfileUtil.Get(this.applicationName, new Guid(user.ProviderUserKey.ToString()));
                    if (profile != null)
                        txtNguoiNhap.Text = profile.Name;
                    else txtNguoiNhap.Text = objPhieuBanHang.NguoiLapPhieu;
                }
                else txtNguoiNhap.Text = objPhieuBanHang.NguoiLapPhieu;
                KhachHang objKH = KhachHangUtil.Get(objPhieuBanHang.MaKhachHang);
                if (objKH != null)
                {
                    txtDiaChi.Text = objKH.DiaChi;
                    txtTenKhachHang.Text = objKH.TenKhachHang;
                }
                //get List Data
                lstChiTiet = ChiTietPhieuBanHangUtil.GetByMaPhieuBanHang(this.MaPhieuBan);
                //generate form here
            }
        }

        #endregion

        #region Methos

        private void InitParamater()
        {
            this.MaPhieuBan = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "id"));
            this.Command = CommonFunction.GetValueFromQueryString(this.Page, "act");
            if (this.Command == string.Empty)
                this.Command = CommonFunction.DEFAULT_COMMAND_ADD;
            if (this.Command == CommonFunction.DEFAULT_COMMAND_ADD)
            {
                btnXemBanIn.Visible = false;
            }
            else
            {
                btnXemBanIn.Visible = true;
            }
        }
        private void InitAutoCompleteData()
        {
            List<MatHangResult> lstMH = new List<MatHangResult>();
            lstMH = MatHangUtil.GetAll();
            string item = string.Empty;
            for (int i = 0; i < lstMH.Count; i++)
            {
                MatHangResult objMH = lstMH[i];
                //foreach (MatHangResult objMH in lstMH)
                //{
                if (i == lstMH.Count - 1)
                    item += "'" + objMH.TenMatHang + "_" + objMH.MaMatHang + "'";
                else item += "'" + objMH.TenMatHang + "_" + objMH.MaMatHang + "',";
            }
            string scriptATCL = @"<script>
            var customarray = new Array(" + item + @");            
            </script>";
            item = string.Empty;
            for (int i = 1; i <= indexGenControl; i++)
            {
                item += "var obj" + i + " = actb(document.getElementById('txtTenMH_" + i + "'), customarray); ";
            }
            scriptATCL += @" <script>
           " + item + @"           
        </script>";
            //            scriptATCL = @" <script>
            //                        var obj1 = actb(document.getElementById('txtTenMH_1'), customarray);
            //            var obj2 = actb(document.getElementById('txtTenMH_2'), customarray);
            //                       
            //                    </script>";
            ltrScript.Text = scriptATCL;
            // ltrScript.Text = "<script>javascript:alert('1111');</script>";

        }

        private void InitTextControl()
        {

            btnSave.Text = Resources.Resource.LABEL_BUTTON_SAVE;
            btnCacel.Text = Resources.Resource.LABEL_BUTTON_CANCEL;
            btnReset.Text = Resources.Resource.LABEL_BUTTON_RESET;
            UserProfile user = UserProfileUtil.Get(this.applicationName, this.userloginId);
            if (user != null)
                txtNguoiNhap.Text = user.Name;
            this.userNameLogin = this.userLoginName;
            txtNgayLapPhieu.Text = ConvertValueObject.ParseString(CommonFunction.Now);
        }


        int wLoaiMH = 140;
        int wTenMH = 350;
        int wXe = 100;
        int wMaMH = 80;
        int wNuocSX = 90;
        int wHieu = 120;
        int wSoLuong = 80;
        int wSoLuongTon = 70;
        int wDVT = 70;
        int wGia = 130;
        int WDel = 20;
        int wMaPT = 180;
        private void GenarateListFormInsert()
        {
            //InitData();
            int maxRowGenerate = 0;
            int maxRowShow = 0;
            hidTotalRow.Value = CommonFunction.PHIEUXUAT_MAXROW_GENNARATE.ToString();
            maxRowGenerate = CommonFunction.PHIEUXUAT_MAXROW_GENNARATE;
            maxRowShow = CommonFunction.PHIEUXUAT_MAXROW_SHOW;
            hidCurrentRowShow.Value = maxRowShow.ToString();
            hidMaxRowShow.Value = maxRowShow.ToString();
            string result = @"<table class='lvt listForm' border='0' cellpadding='3' cellspacing='1' width='100%'>                    
                    <tr>
                     
                        <td class='lvtCol' style='width: " + wMaMH + @"px;'>
                            Mã MH
                        </td>
                        <td class='lvtCol' style='width: " + wMaPT + @"px;'>
                            Mã phụ tùng
                        </td>
                        <td class='lvtCol' style='width: " + wTenMH + @"px;'>
                            Tên mặt hàng
                        </td>
                    <td class='lvtCol' style='width: " + wXe + @"px;'>
                            Xe
                        </td>
                       
                               <td class='lvtCol' style='width: " + wDVT + @"px;'>
                            DVT
                        </td>   
                        
                        <td class='lvtCol' style='width: " + wSoLuongTon + @"px;'>
                            Số lượng tồn
                        </td>
                    <td class='lvtCol' style='width: " + wSoLuong + @"px;'>
                            Số lượng bán
                        </td>
                        <td class='lvtCol' style='width: " + wGia + @"px;'>
                            Giá đơn vị 
                        </td>
 <td class='lvtCol' style='width: " + wGia + @"px;'>
                            Thành tiền
                        </td>
                        <td class='lvtCol' style='width: " + WDel + @"px;'>
                        </td>
                    </tr>";
            for (int i = 1; i <= maxRowGenerate; i++)
            {
                if (i <= maxRowShow)
                    result += GenarateRowInsert(i, "");
                else result += GenarateRowInsert(i, "none");
            }
            result += @"</table>";
            tdForm.InnerHtml = result;
            indexGenControl = maxRowGenerate;

        }
        private void GenarateListFormUpdate()
        {
            int maxRowGenerate = 0;
            int maxRowShow = 0;
            if (lstChiTiet.Count > CommonFunction.PHIEUXUAT_MAXROW_GENNARATE)
            {
                maxRowGenerate = lstChiTiet.Count;
                maxRowShow = maxRowGenerate;
            }
            else
            {
                maxRowGenerate = CommonFunction.PHIEUXUAT_MAXROW_GENNARATE;
                maxRowShow = lstChiTiet.Count;
            }
            hidTotalRow.Value = maxRowGenerate.ToString();
            hidMaxRowShow.Value = maxRowShow.ToString();
            hidCurrentRowShow.Value = maxRowShow.ToString();
            string result = @"<table class='lvt listForm' border='0' cellpadding='3' cellspacing='1' width='100%'>                    
                    <tr>
                     
                        <td class='lvtCol' style='width: " + wMaMH + @"px;'>
                            Mã MH
                        </td>
                        <td class='lvtCol' style='width: " + wMaPT + @"px;'>
                            Mã phụ tùng
                        </td>
                        <td class='lvtCol' style='width: " + wTenMH + @"px;'>
                            Tên mặt hàng
                        </td>
                        <td class='lvtCol' style='width: " + wLoaiMH + @"px;'>
                            Loại mặt hàng
                        </td>
                               <td class='lvtCol' style='width: " + wDVT + @"px;'>
                            DVT
                        </td>   
                        
                        <td class='lvtCol' style='width: " + wSoLuongTon + @"px;'>
                            Số lượng tồn
                        </td>
                    <td class='lvtCol' style='width: " + wSoLuong + @"px;'>
                            Số lượng bán
                        </td>
                        <td class='lvtCol' style='width: " + wGia + @"px;'>
                             Giá đơn vị 
                        </td>
  <td class='lvtCol' style='width: " + wGia + @"px;'>
                            Thành tiền
                        </td>
                        <td class='lvtCol' style='width: " + WDel + @"px;'>
                        </td>
                    </tr>";
            //get list LOaiHH


            //List<SubCategoryResult> lstDVT = SubCategoryUtil.GetAllByCategory(CommonFunction.DEFAULT_UNIT_CODE);
            for (int i = 1; i <= maxRowGenerate; i++)
            {
                if (i <= maxRowShow)
                    result += GenarateRowUpdate(i, "");
                else result += GenarateRowUpdate(i, "none");
            }
            result += @"</table>";
            tdForm.InnerHtml = result;
            txtTongThanhTien.Value = ConvertValueObject.ParseString(sum);
            indexGenControl = maxRowGenerate;
        }
        double sum = 0;
        private string GenarateRowUpdate(int indexRow, string display)
        {
            ChiTietPhieuBanHangResult objData = null;
            try
            {
                objData = lstChiTiet[indexRow - 1];
            }
            catch { }
            string optionDVT = string.Empty;
            string dvtControl = string.Empty;
            string result = string.Empty;
            //  double sum = 0;
            if (objData == null)
            {
                optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + indexRow + ",this.value)' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";
                result = @"<tr style='display:" + display + "' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>    
                        <td>
                            <input type='text' value='' onkeypress='GetMatHang(event," + indexRow + ",this.value);' id='txtMaMH_" + indexRow + @"'  style='width:98%;' onblur='TextBoxLeftBlur(this);' onfocus='TextBoxLeftFocus(this);' class='detailedViewTextBoxList1'/>
                        </td>
                    <td>
                            <input type='text' value='' autocomplete='off' onkeypress='GetMatHangByMaPhuTung(event," + indexRow + @",this.value);' id='txtMaPhuTung_" + indexRow + @"'  style='width:98%' class='detailedViewTextBoxList ctrListForm' />
                        </td> 
                        <td>
                             <input type='text' value='' onkeypress='GetMatHangByName(event," + indexRow + @",this.value);' autocomplete='off' id='txtTenMH_" + indexRow + @"'   style='width:" + (wTenMH) + @"px' class='detailedViewTextBoxList ctrListForm' />
        
                        </td>
 
                       <td>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>   
                       
                        <td id='tdDVT_" + indexRow + @"' >
                            " + dvtControl + @"
                        </td>
                       <td>
                            <input type='text' value='' id='txtSoLuongTon_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);' />
<input type='hidden' value='0' id='txtSoLuongTonTemp_" + indexRow + @"'  />
                        </td>
                        <td>
                            <input type='text' value='1'  onblur='CheckSoLuongBan(this.value," + indexRow + @"); GetThanhTien(" + indexRow + ");' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);'/>
                        </td>
                       
                       
                        <td>
                            <input type='text' value='' MaxLength='14' onblur='this.value=formatNumberAutoSeparate(this.value); GetThanhTien(" + indexRow + ");' id='txtGiaNhap_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this);' />
                        </td>
                        <td>
                            <input type='text' value='' MaxLength='14'  id='txtThanhTien_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                />
                        </td>
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a> 
                        </td>            
                    </tr>";
                return result;
            }
            //get MatHang Detail
            MatHang objMatHang = MatHangUtil.Get(objData.MaMatHang);
            if (objMatHang == null)
                return string.Empty;


            //get DVT
            List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(objData.MaMatHang);
            if (lstDVT != null)
            {

                optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                foreach (SubCategoryResult objDVT in lstDVT)
                {
                    if (objDVT.SubCategoryId == objData.MaDonViTinh)
                        optionDVT += "<option selected='selected' value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                    else optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                }

                dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + indexRow + ",this.value)' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

            }
            string xe = string.Empty;
            DMXe objDMXe = DMXeUtil.Get(objMatHang.MaXe);
            if (objDMXe != null)
            {
                // SubCategory objSeriXe = SubCategoryUtil.Get(objDMXe.MaXe, CommonFunction.DEFAULT_CHUNGLOAIXE_CODE);
                //  if (objSeriXe != null)
                xe = objDMXe.TenXe;

            }

            int slTon = MatHangUtil.GetSoLuongTonByDonViTinh(objMatHang.MaMatHang, ConvertValueObject.ParseInt(drlKho.SelectedValue), objData.MaDonViTinh);
            double thanhTien = objData.GiaBan * objData.SoLuong;
            sum += thanhTien;

            result = @"<tr style='display:" + display + "' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>    
                        <td>
                            <input type='text' value='" + objData.MaMatHang + @"' onkeypress='GetMatHang(event," + indexRow + ",this.value);' id='txtMaMH_" + indexRow + @"'  style='width:98%;' onblur='TextBoxLeftBlur(this);' onfocus='TextBoxLeftFocus(this);' class='detailedViewTextBoxList1'/>
                        </td>
                    <td>
                            <input type='text' value='" + objMatHang.MaPhuTung + @"' autocomplete='off' onkeypress='GetMatHangByMaPhuTung(event," + indexRow + @",this.value);' id='txtMaPhuTung_" + indexRow + @"'  style='width:98%' class='detailedViewTextBoxList ctrListForm' />
                        </td> 
                        <td>
                           <input type='text' value='" + objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + "' id='txtTenMH_" + indexRow + @"' onkeypress='GetMatHangByName(event," + indexRow + @",this.value);' autocomplete='off' style='width:" + (wTenMH) + @"px' class='detailedViewTextBoxList ctrListForm'/>
                              
                        </td>
 
                        <td>
                            <input type='text' value='" + xe + @"' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>   
                       
                        <td id='tdDVT_" + indexRow + @"' >
                            " + dvtControl + @"
                        </td>
                       <td>
                            <input type='text' value='" + slTon + @"' id='txtSoLuongTon_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);' />
                            <input type='hidden' value='" + objData.SoLuong + @"' id='txtSoLuongTonTemp_" + indexRow + @"'  />
                        </td>
                        <td>
                            <input type='text' value='" + objData.SoLuong + @"'  onblur='CheckSoLuongBan(this.value," + indexRow + @"); GetThanhTien(" + indexRow + ");' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);' />
                        </td>
                       
                       
                        <td>
                            <input type='text' value='" + ConvertValueObject.ParseString(objData.GiaBan) + @"' MaxLength='14' onblur='this.value=formatNumberAutoSeparate(this.value); GetThanhTien(" + indexRow + ");' id='txtGiaNhap_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this);'/>
                        </td>
                        <td>
                            <input type='text' value='" + ConvertValueObject.ParseString(thanhTien) + @"' MaxLength='14'  id='txtThanhTien_" + indexRow + @"' style='width:98%' disabled='disabled' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                />
                        </td>
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a> 
                        </td>            
                    </tr>";

            return result;

        }
        private string CreateTDMaPTUpdate(string maPT, int rowIndex)
        {
            if (maPT != string.Empty)
                return @"
              <td>
                            <input type='text' disabled='false' value='" + maPT + @"' autocomplete='off' onkeypress='GetMatHangByMaPhuTung(event," + rowIndex + @",this.value);' id='txtMaPhuTung_" + rowIndex + @"'  style='width:98%' class='detailedViewTextBoxList ctrListForm' />
                        </td>";
            else
                return @"
              <td>
                            <input type='text' disabled='true' value='" + maPT + @"' autocomplete='off' onkeypress='GetMatHangByMaPhuTung(event," + rowIndex + @",this.value);' id='txtMaPhuTung_" + rowIndex + @"'  style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>";
        }
        private string GenarateRowInsert(int indexRow, string display)
        {
            string strOptionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";


            string result = @"<tr style='display:" + display + @";' id='tr_" + indexRow + @"' bgcolor='white' onmouseout='RowOut(this);' onmouseover='RowHover(this);'
                        class='lvtColData'>
                        <td>
                            <input type='text' value='' onkeypress='GetMatHang(event," + indexRow + ",this.value);' id='txtMaMH_" + indexRow + @"'  style='width:98%;' onblur='TextBoxLeftBlur(this);' onfocus='TextBoxLeftFocus(this);' class='detailedViewTextBoxList1' />
                        </td>
 <td>
                            <input type='text' value='' autocomplete='off' onkeypress='GetMatHangByMaPhuTung(event," + indexRow + @",this.value);' id='txtMaPhuTung_" + indexRow + @"'  style='width:98%' class='detailedViewTextBoxList ctrListForm' />
                        </td>  
                        <td>
                           <input type='text' value='' onkeypress='GetMatHangByName(event," + indexRow + @",this.value);' autocomplete='off' id='txtTenMH_" + indexRow + @"'   style='width:" + (wTenMH) + @"px' class='detailedViewTextBoxList ctrListForm' />
                        </td> 
                           <td>
                            <input type='text' value='' id='txtXe_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm' />
                        </td>                
                        <td id='tdDVT_" + indexRow + @"' >
                            <select class='listForm ctrListForm' id='drlDonViTinh_" + indexRow + @"'  style='width:98%'>  " + strOptionDVT + @"                             
                            </select>
                        </td>
                       <td>
                            <input type='text' value='' id='txtSoLuongTon_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);' />
                            <input type='hidden' value='0' id='txtSoLuongTonTemp_" + indexRow + @"'  />
                        </td>
                        <td>
                            <input type='text' value='1'  onblur='CheckSoLuongBan(this.value," + indexRow + @"); GetThanhTien(" + indexRow + ");' id='txtSoLuong_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlur(this);'/>
                        </td>
                       
                       
                        <td>
                            <input type='text' value='' MaxLength='14' onblur='this.value=formatNumberAutoSeparate(this.value); GetThanhTien(" + indexRow + @");' id='txtGiaNhap_" + indexRow + @"' style='width:98%' class='detailedViewTextBoxList1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this);'    />
                        </td>
                        <td>
                            <input type='text' value='' MaxLength='14' id='txtThanhTien_" + indexRow + @"' disabled='disabled' style='width:98%' class='detailedViewTextBoxList_Disable1 ctrListForm txtNumber'
                                onfocus='TextBoxFocus(this);' onblur='TextBoxBlurGroupNumber(this);'
                                  />
                        </td>
                        <td align='center'>
                            <a href='javascript:void(0);' onClick='DeleteRow(" + indexRow + @")'> Xóa</a> 
                        </td>
                    </tr>";
            return result;
        }



        #endregion

        #region Events, Handler


        protected void btnXemBanIn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewPhieuBanHang.aspx?id=" + this.MaPhieuBan);
        }
        protected void btnCacel_Click(object sender, EventArgs e)
        {
            Response.Redirect(urlList);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.GetCurrentPageAndParamaterUrl());
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {


        }
        #endregion

        [Ajax.AjaxMethod()]
        public string GeMatHangInforByMaPT(string maPT, string row, string kho)
        {
            int dvt = 0;
            MatHang objMatHang = MatHangUtil.GetMatHangByMaPhuTungInPhieuNhaphang(maPT);
            if (objMatHang != null)
            {

                string optionDVT = string.Empty;
                string dvtControl = string.Empty;
                //get DVT
                List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(objMatHang.MaMatHang);
                if (lstDVT != null)
                {

                    optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                    foreach (SubCategoryResult objDVT in lstDVT)
                    {
                        if (objDVT.SubCategoryId == objMatHang.DVT)
                        {
                            dvt = objDVT.SubCategoryId;
                            optionDVT += "<option selected='selected'  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                        }
                        else
                        {
                            optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";

                        }
                    }

                    dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + row + ",this.value)' id='drlDonViTinh_" + row + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

                }

                //SubCategory objLoaiHH = SubCategoryUtil.Get(objMatHang.MaLoaiHangHoa, CommonFunction.DEFAULT_LOAIHANGHOA_CODE);

                // if (objLoaiHH != null)
                //{
                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                    xe = objXe.TenXe;
                int result = MatHangUtil.GetSoLuongTonByDonViTinh(objMatHang.MaMatHang, ConvertValueObject.ParseInt(kho), dvt);
                return objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + "*|*" + objMatHang.MaPhuTung + "*|*" + dvtControl + "*|*" + ConvertValueObject.ParseString(objMatHang.GiaBan) + "*|*" + result + "*|*" + objMatHang.MaMatHang + "*|*" + objXe.TenXe;
                //}

            }
            else return string.Empty;
        }

        [Ajax.AjaxMethod()]
        public string GeMatHangInfor(string maMatHang, string row, string kho)
        {
            int dvt = 0;
            MatHang objMatHang = MatHangUtil.Get(maMatHang);
            if (objMatHang != null)
            {
                string optionDVT = string.Empty;
                string dvtControl = string.Empty;
                //get DVT
                List<SubCategoryResult> lstDVT = SubCategoryUtil.GetDonViTinhByMatHang(maMatHang);
                if (lstDVT != null)
                {

                    optionDVT = "<option value='" + string.Empty + @"'> " + CommonFunction.DEFAULT_COMBOBOX_EMPTY_TEXT + @"</option>";
                    foreach (SubCategoryResult objDVT in lstDVT)
                    {
                        if (objDVT.SubCategoryId == objMatHang.DVT)
                        {
                            dvt = objDVT.SubCategoryId;
                            optionDVT += "<option selected='selected'  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";
                        }
                        else
                        {
                            optionDVT += "<option  value='" + objDVT.SubCategoryId + "'>" + objDVT.SubCategoryName + "</option>";

                        }
                    }

                    dvtControl = @"<select class='listForm ctrListForm' onkeyup='this.blur();this.focus();' onChange='CheckSoLuongTonMatHang(" + row + ",this.value)' id='drlDonViTinh_" + row + @"'  style='width:98%'>  " + optionDVT + @"                             
                            </select>";

                }

                //SubCategory objLoaiHH = SubCategoryUtil.Get(objMatHang.MaLoaiHangHoa, CommonFunction.DEFAULT_LOAIHANGHOA_CODE);

                // if (objLoaiHH != null)
                //{
                string xe = string.Empty;
                DMXe objXe = DMXeUtil.Get(objMatHang.MaXe);
                if (objXe != null)
                    xe = objXe.TenXe;
                int result = MatHangUtil.GetSoLuongTonByDonViTinh(maMatHang, ConvertValueObject.ParseInt(kho), dvt);
                return objMatHang.TenMatHang + "_" + objMatHang.MaMatHang + "*|*" + objMatHang.MaPhuTung + "*|*" + dvtControl + "*|*" + ConvertValueObject.ParseString(objMatHang.GiaBan) + "*|*" + result + "*|*" + objMatHang.MaMatHang + "*|*" + objXe.TenXe;
                //}
            }
            return string.Empty;

        }
        [Ajax.AjaxMethod()]
        public string GetSoLuongTon(string maMH, string makho, string maDVT)
        {
            int result = MatHangUtil.GetSoLuongTonByDonViTinh(maMH, ConvertValueObject.ParseInt(makho), ConvertValueObject.ParseInt(maDVT));
            return result.ToString();
        }
        [Ajax.AjaxMethod()]
        public string InsertData(string command, string maphieu, string kho, string nguoiLapPhieu, string ngayLapPhieu, string tenKH, string diaChi, string data)
        {
            DateTime now = CommonFunction.Now;
            //MaMH|DVT|SL|GiaNhap            
            string[] arrRow = data.Split('*');
            if (arrRow.Length > 0)
            {
                double total = 0;
                List<ChiTietPhieuBanHang> lstChiTiet = new List<ChiTietPhieuBanHang>();
                for (int i = 0; i < arrRow.Length - 1; i++)
                {
                    ChiTietPhieuBanHang objChiTiet = GetRowData(arrRow[i].ToString());
                    total += objChiTiet.GiaBan * objChiTiet.SoLuong;
                    lstChiTiet.Add(objChiTiet);
                }
                KhachHang objKH = new KhachHang();
                objKH.TenKhachHang = tenKH;
                objKH.DiaChi = diaChi;
                PhieuBanHang objPhieuNhap = new PhieuBanHang();
                Int64 phieuNhap = ConvertValueObject.ParseLong(maphieu);
                if (command == CommonFunction.DEFAULT_COMMAND_EDIT)
                    objPhieuNhap = PhieuBanHangUtil.Get(phieuNhap);
                objPhieuNhap.MaKho = ConvertValueObject.ParseInt(kho);
                objPhieuNhap.NgayBan = objPhieuNhap.NgayLapPhieu = CommonFunction.SetTime(ConvertValueObject.ParseDateTime(ngayLapPhieu),now.Hour,now.Minute,now.Second);
                objPhieuNhap.NguoiBan = objPhieuNhap.NguoiLapPhieu = nguoiLapPhieu;
                objPhieuNhap.TongGiaTien = total;
                objPhieuNhap.SysCreatedBy = nguoiLapPhieu;
                objPhieuNhap.SysCreatedDate = now;
                objPhieuNhap.SysUpdatedBy = nguoiLapPhieu;
                objPhieuNhap.SysUpdatedDate = now;
                //  objPhieuNhap.
                //  objPhieuNhap.NguoiNhap = nguoinhap;
                if (command == CommonFunction.DEFAULT_COMMAND_EDIT)
                {
                    if (phieuNhap > 0)
                    {
                        objKH.MaKhachHang = objPhieuNhap.MaKhachHang;
                        objPhieuNhap.MaPhieuBanHang = phieuNhap;
                        if (PhieuBanHangUtil.Update(objPhieuNhap, objKH, lstChiTiet))
                            return phieuNhap.ToString();
                        return "0";
                    }
                    else return "0";
                }
                else
                {
                    Int64 id = PhieuBanHangUtil.Insert(objPhieuNhap, objKH, lstChiTiet);
                    if (id > 0)
                        return id.ToString();
                    return "0";
                }

            }
            return "0";

        }
        private ChiTietPhieuBanHang GetRowData(string data)
        {
            string[] arr = data.Split(';');
            if (arr.Length > 0)
            {
                ChiTietPhieuBanHang objData = new ChiTietPhieuBanHang();
                objData.MaMatHang = arr[0].ToString();
                objData.SoLuong = ConvertValueObject.ParseInt(arr[1].ToString());
                objData.MaDonViTinh = ConvertValueObject.ParseInt(arr[2].ToString());
                objData.GiaBan = ConvertValueObject.ParseDouble(arr[3].ToString());
                objData.CurencyCode = CommonFunction.DEFAULT_VND_CURENCY;
                return objData;
            }
            return null;
        }
        private int GetSoLuongTonTempByMH(List<ChiTietPhieuBanHangResult> lstDetail, string maMH, int dvt)
        {
            try
            {
                foreach (ChiTietPhieuBanHangResult objData in lstDetail)
                {
                    if (objData.MaMatHang == maMH && objData.MaDonViTinh == dvt)
                        return objData.SoLuong;
                }
            }
            catch
            { }
            return 0;
        }
        [Ajax.AjaxMethod()]
        public string GetListSoLuongTon(string maPhieu, string lstMH, string lstDVT, string makho)
        {
            int kho = ConvertValueObject.ParseInt(makho);
            List<ChiTietPhieuBanHangResult> lstDetail = null;
            PhieuBanHang objPhieuBanHang = PhieuBanHangUtil.Get(ConvertValueObject.ParseInt(maPhieu));
            if (objPhieuBanHang != null && objPhieuBanHang.MaKho == kho)
                lstDetail = ChiTietPhieuBanHangUtil.GetByMaPhieuBanHang(ConvertValueObject.ParseInt(maPhieu));
            string arrSLTon = string.Empty;
            string arrSLTemp = string.Empty;
            string[] arrMH = lstMH.Split('#');
            string[] arrDVT = lstDVT.Split('#');
            if (arrMH.Length > 1)
            {
                int dvt = 0;
                for (int i = 0; i < arrMH.Length - 1; i++)
                {
                    dvt = ConvertValueObject.ParseInt(arrDVT[i].ToString());
                    if (arrMH[i].ToString() != string.Empty && dvt > 0)
                        arrSLTon += MatHangUtil.GetSoLuongTonByDonViTinh(arrMH[i].ToString(), ConvertValueObject.ParseInt(makho), dvt) + "|";
                    else arrSLTon += "0|";


                    arrSLTemp += GetSoLuongTonTempByMH(lstDetail, arrMH[i].ToString(), dvt) + "|";

                }
            }

            return arrSLTon + "*" + arrSLTemp;
        }

    }
}
